package com.example.leave.exception;

import com.example.leave.common.CommonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.DefaultMessageSourceResolvable;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.stream.Collectors;

@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义的业务异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
    public CommonResult<Exception> bizExceptionHandler(HttpServletRequest req, BizException e) {
        logger.error("发生业务异常！", e);
        return CommonResult.error(e, e.getErrorMsg());
    }

    /**
     * 处理空指针的异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    @ResponseBody
    public CommonResult<String> exceptionHandler(HttpServletRequest req, NullPointerException e) {
        logger.error("发生空指针异常！原因是:", e);
        return CommonResult.error(e.getMessage(), "发生空指针异常！");
    }


    /**
     * 处理其他异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public CommonResult<Exception> exceptionHandler(HttpServletRequest req, Exception e) {
        logger.error("未知异常！原因是:", e);
        return CommonResult.error(e, "未知异常！");
    }

    @ExceptionHandler(value = DataIntegrityViolationException.class)
    @ResponseBody
    public CommonResult<String> constraintViolationException(HttpServletRequest req, DataIntegrityViolationException e) {
        logger.error("所传参数不符合规范！原因是:", e);
        return CommonResult.error(e.getMessage(), "约束冲突异常！");
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public CommonResult<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
        String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
        return CommonResult.error(ExceptionEnum.ILLEGAL_PARAMETER.getMessage(), message);
    }

    @ExceptionHandler(EmptyResultDataAccessException.class)
    @ResponseBody
    public CommonResult<String> emptyResultDataAccessExceptionHandler(EmptyResultDataAccessException e) {
        String message = e.getMessage();
        return CommonResult.error(ExceptionEnum.NOT_FOUND.getMessage(), message);
    }


}